package control.micro
{
	import events.BattleEvent;
	import events.GuiEvent;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import model.micro.ArcherUnit;
	import model.micro.Battle;
	import util.Settings;
	import view.micro.BattleSprite;
	
	/**
	 * The controller object for a human player during a battle.
	 */
	public class HumanBattleController extends BattleController
	{
		protected var mouse_is_down:Boolean;
		protected var mouse_position:Point;
		protected var keys:Array;
		
		protected const K_SPEED_UP:int = 187;
		protected const K_SPEED_DOWN:int = 189;
		protected const K_SCROLL_LEFT:int = 37;
		protected const K_SCROLL_RIGHT:int = 39;
		
		/**
		 *
		 * @param	battle			The battle this is controlling.
		 * @param	battleSprite	The sprite creating the events.
		 */
		public function HumanBattleController(battle:Battle, stage:Stage)
		{
			super(battle);
			
			mouse_position = new Point();
			keys = new Array();
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			battle.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			battle.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			battle.addEventListener(MouseEvent.MOUSE_WHEEL, onScroll);
		}
		
		/**
		 * Update every frame.
		 */
		override public function update():void
		{
			super.update();
		}
		
		/**
		 * Scroll the camera.
		 * @param	e
		 */
		protected function onScroll(e:MouseEvent):void
		{
			battle.dispatchEvent(new GuiEvent(GuiEvent.CAMERA_ZOOM, {z: e.delta / 10}));
			battle.dispatchEvent(new GuiEvent(GuiEvent.CAMERA_SCROLL_TO, {x: e.localX, y: e.localY, center: true}));
		}
		
		/**
		 * Update the mouse position.
		 * @param	e
		 */
		protected function onMouseMove(e:MouseEvent):void
		{
			mouse_position.x = e.localX;
			mouse_position.y = e.localY;
		}
		
		/**
		 * Handles mouse down events.
		 * @param	e	the event
		 */
		protected function onMouseDown(e:MouseEvent):void
		{
			mouse_is_down = true;
		}
		
		/**
		 * Handle mouse up events.
		 * @param	e
		 */
		protected function onMouseUp(e:MouseEvent):void
		{
			mouse_is_down = false;
		}
		
		/**
		 * Handle keydown events.
		 * @param	e	the event
		 */
		protected function onKeyDown(e:KeyboardEvent):void
		{
			keys[e.keyCode] = true;
			
			switch (e.keyCode)
			{
				case K_SPEED_UP: 
					Settings.time_warp += 1;
					break;
				case K_SPEED_DOWN: 
					Settings.time_warp -= 1;
					break;
				case K_SCROLL_LEFT: 
					battle.dispatchEvent(new GuiEvent(GuiEvent.CAMERA_SCROLL, {x: -10, y: 0}));
					break;
				case K_SCROLL_RIGHT: 
					battle.dispatchEvent(new GuiEvent(GuiEvent.CAMERA_SCROLL, {x: 10, y: 0}));
					break;
			}
		}
		
		/**
		 * Handle keyup events
		 * @param	e	the event
		 */
		protected function onKeyUp(e:KeyboardEvent):void
		{
			keys[e.keyCode] = false;
		}
		
		/**
		 * Deconstruct the controller object.
		 */
		public function destroy():void
		{
			battle.removeEventListener(MouseEvent.MOUSE_UP, onMouseDown);
			battle.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseDown);
		}
	}
}